#!/bin/bash

#-------------------------------------------------------------------------------
# hscCopyFromFloppy.sh
#
# This shell script is invoked to copy a file to a floppy diskette.
#
# Usage: hscCopyFromFloppy <fully qualified target dircetory on HMC hard drive> 
#
# Return Codes:
# 0 - Copy was successful
# Value other than 0 - Copy was not successful
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# The mount point for the floppy.
# A corresponding device entry should exist for this mount point in /etc/fstab
#-------------------------------------------------------------------------------
FD_MOUNTPOINT=/mnt/floppy/

# initialize to "fail"
RPMRC=1

# Mount the floppy device
mount -v $FD_MOUNTPOINT 

if [ $? -eq 0 ] ; then
    # The mount command was successful. Go to target directory
    cd $FD_MOUNTPOINT

    # Copy every RPM file from the diskette to the target
    # directory staging/working location
    for f in *.rpm
    do
       if [ -f $f ] ; then
          cp -f $f $1
          
          if [ $? -eq 0 ] ; then
              # The file was copied successfully. Continue with RPM to unpack the
              # service file.  Force changed data blocks to disk with 'sync' cmd.
              cd /
              sync
              chmod 777 $1$f
              rpm -i $1$f --force 2> /tmp/rpm.log
              RPMRC=$?
              chmod 777 /tmp/rpm.log
              
              if [ $RPMRC -ne 0 ] ; then
                 umount $FD_MOUNTPOINT
                 if [ $? -eq 0 ] ; then
                     # RPM cmd was not successful, unmount floppy OK
                     # Remove the rpm file before exiting
                     rm -f $1$f
                     exit 4
                 else
                     # RPM cmd was not successful, unmount also unsuccessful
                     # Remove the rpm file before exiting
                     rm -f $1$f
                     exit 5
                 fi
              fi
          else
              # The file was not copied successfully.
              cd /
              umount $FD_MOUNTPOINT
              if [ $? -eq 0 ] ; then
                  # Copy of service file unsuccessful, unmount floppy successful
                  exit 6
              else
                  # Copy of service file unsuccessful, unmount floppy also unsuccessful
                  exit 7
              fi
          fi

          # Back to removable media mount point
          cd $FD_MOUNTPOINT
       fi
    done
    
    if [ $RPMRC -eq 0 ] ; then
       # All copies and RPM cmds successful
       cd /
       umount $FD_MOUNTPOINT
       if [ $? -eq 0 ] ; then
           # RPM cmd successful, unmount floppy successful
           # This is the only "good" way out of this script
           exit 0
       else
           # RPM cmd successful, unmount floppy unsuccessful
           exit 3
       fi
    else
       # no RPM file(s) transferred
       exit 8
    fi
      
else
    # The mount command failed.
    grep $FD_MOUNTPOINT /etc/mtab
    if [ $? -eq 0 ] ; then
        # The floppy is already mounted.
        exit 1
    else
        # The floppy is not already mounted, but error mounting.
        exit 2
    fi
fi

